<!-- 工资列表 -->
<template>
  <div class="salary-list">
    <div ref="dgtools" class="filter-container">
      <div class="dgts-row">
        <el-form :model="dg.ps" :inline="true" @keyup.enter.native="getDgList">
          <el-form-item>
            <el-select v-model="dg.ps.driverInfo" clearable filterable placeholder="请选择员工" style="width: 120px">
              <el-option v-for="it in staffOptions" :key="it.leaveUname" :label="it.realName" :value="it.leaveUname">
                <span>{{ it.realName }}</span>
                <span>{{ it.phone }}</span>
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-input style="width: 120px" placeholder="订单号/订单详情" v-model="dg.ps.orderInfo">
            </el-input>
          </el-form-item>

          <el-form-item>
            <el-select v-model="dg.ps.status" placeholder="收支状态" clearable style="width: 150px">
              <el-option label="收入" value="0">
              </el-option>
              <el-option label="支出" value="1">
              </el-option>

            </el-select>
          </el-form-item>
          <el-form-item>
            <el-date-picker style="width: 205px" v-model="dg.ps.sedate" type="daterange" range-separator="至"
              start-placeholder="开始日期" end-placeholder="结束日期" value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item>
          <el-form-item>
            <el-select v-model="dg.ps.salaryStatus" placeholder="审核状态" clearable style="width: 150px">
              <el-option v-for="it in salaryStatusArr" :key="it.value" :label="it.label" :value="it.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="getDgList">
            搜索
          </el-button>

          <el-button type="primary" icon="el-icon-download" @click="salaryExcelTpDown">
            工资模版下载
          </el-button>

          <el-button type="primary" icon="el-icon-upload2" @click="salaryBalanceImport">
            工资余额导入
          </el-button>

          <el-button type="primary" icon="el-icon-s-order" @click="staffBzExport">
            生成员工报账
          </el-button>
          <el-tag type="danger" style="margin-left: 10px;font-weight: 700;height:25px;line-height:25px">总金额：{{
            totalMoney
          }}</el-tag>
          <el-tag type="success" style="margin-left: 10px;font-weight: 700;height:25px;line-height:25px">已操作：{{
            operMoney
          }}</el-tag>
          <el-tag type="warning" style="margin-left: 10px;font-weight: 700;height:25px;line-height:25px">未操作：{{
            notOperMoney
          }}</el-tag>

        </el-form>
      </div>

      <TableSelfSet v-if="dg.columns.length > 0" :tbTitle="'工资列表'" :dgName="dg.dgName" :tbId="dg.tbName"
        :srcCols="dg.srcCols" :columns="dg.columns" @saveSet="saveTbSet" />
    </div>

    <el-table :ref="dg.tbName" :data="dg.list" row-key="id" :height="bodyHeight" border fit :row-class-name="rowClassName"
      @header-dragend="dragHeader" @selection-change="selRowChange">
      <el-table-column fixed="left" type="index" label="编号" align="center" :resizable="false" width="40">
      </el-table-column>
      <el-table-column fixed="left" type="selection" align="center" :resizable="false" width="40">
      </el-table-column>
      <!-- <el-table-column fixed="left" type="selection" width="40">
      </el-table-column> -->
      <el-table-column v-for="(col, index) in dg.columns" :key="index" :class-name="dg.dgName" :prop="col.field"
        :label="col.label" :width="col.width">
        <template slot-scope="{row}">
          <template v-if="col.field == 'baseUserId'">
            <span>
              {{ row.baseUserId.realName + '-' + row.baseUserId.phone }}
            </span>
          </template>
          <template v-else-if="col.field == 'atype'">
            <span>
              {{ row.atype == 1 ? "余额" : row.atype == 2 ? "冻结巴士币" : row.atype == 3 ? "解冻巴士币" : row.atype == 4 ? "第三方金额" :
                row.atype == 5 ? "派单余额" : row.atype == 6 ? "抢单余额" : row.atype == 7 ? "垫支余额" : row.atype == 8 ? "下账余额" :
                  row.atype ==
                    9 ? "信用额度" : row.atype == 10 ? "工资余额" : '金额类型错误' }}
            </span>
          </template>
          <template v-else-if="col.field == 'status'">
            <span>
              {{ row.status == 0 ? "收入" : "支出" }}
            </span>
          </template>
          <template v-else-if="col.field == 'type'">
            <span>
              {{ row.type == 17 ? "驾驶员工资" : "驾驶员工资" }}
            </span>
          </template>
          <template v-else-if="col.field == 'staffReimburseId'">
            <span>
              {{ row.staffReimburseId ? "已生成员工报账" : "未操作" }}
            </span>
          </template>
          <template v-else> {{ getColVal(row, col.field) }}</template>
        </template>
      </el-table-column>
    </el-table>
    <Pagination v-show="dg.total > 0" :total="dg.total" :page.sync="dg.ps.page" :limit.sync="dg.ps.rows"
      @pagination="getDgList" />

    <input id="file" ref="inputRef" type="file" name="file" style="display: none;" @change="handleChange" />
  </div>
</template>

<script>
import DataGridBase from "@/views/mixins/DataGridBase";
import DataGridResize from "@/views/mixins/DataGridResize";
import elDragDialog from "@/directive/el-drag-dialog";
import TableSelfSet from "@/components/TableSelfSet";
import Pagination from "@/components/Pagination";




import { downFiles } from "@/api/export";

import * as commonSer from "@/api/services/common.service";
import * as logisticsSer from "@/api/services/logistics.service";
import * as orderSer from "@/api/services/order.service";

export default {
  mixins: [DataGridBase, DataGridResize],
  directives: { elDragDialog },
  components: { TableSelfSet, Pagination },
  data() {
    let cols = [
      // { label: '工资时间', field: 'salaryTime', width: 150 },
      // { label: '单位编号', field: 'unitNum', width: 120 },
      // { label: '员工姓名', field: 'baseUser.realName', width: 120 },
      // { label: '车牌号', field: 'plateNum', width: 100 },
      // { label: '工资状态', field: 'salaryStatus', width: 100 },
      // { label: '应领合计', field: 'shouldMoney', width: 100 },
      // { label: '扣社保', field: 'socialMoney', width: 120 },
      // { label: '代支社保金额', field: 'dzSocialMoney', width: 120 },
      // { label: '扣安全互助金', field: 'helpMoney', width: 100 },
      // { label: '扣房租', field: 'rentRoomMoney', width: 100 },
      // { label: '扣水电费', field: 'waterElecMoney', width: 100 },
      // { label: '扣借款', field: 'borrowMoney', width: 120 },
      // { label: '扣擦挂事故个人赔付', field: 'scratchMoney', width: 140 },
      // { label: '扣工作服', field: 'workSuitMoney', width: 140 },
      // { label: '扣公益基金', field: 'charitableMoney', width: 140 },
      // { label: '扣其他', field: 'otherMoney', width: 140 },
      // { label: '实领合计', field: 'actualMoney', width: 140 },
      // { label: '公账', field: 'payMoneyCorporate', width: 140 },
      // { label: '私卡', field: 'payMoneyPerson', width: 140 },
      // { label: '社保', field: 'social', width: 140 },
      // { label: '账户', field: 'account', width: 140 },
      // { label: '备注', field: 'remark', width: 140 },
      { label: '驾驶员', field: 'baseUserId', width: 140 },
      // { label: '电话', field: 'baseUserId.phone', width: 120 },
      { label: '账户类型', field: 'atype', width: 70 },
      { label: '收支状态', field: 'status', width: 70 },
      { label: '金额', field: 'amoney', width: 70 },
      { label: '交易后余额', field: 'cashBalance', width: 80 },
      { label: '行程备注', field: 'note', },
      { label: '订单号', field: 'assist', width: 150 },
      { label: '交易类型', field: 'type', width: 80 },
      { label: '状态', field: 'staffReimburseId', width: 120 },
      { label: '系统备注', field: 'wdcNote', width: 150 },
      { label: '添加时间', field: 'atime', width: 125 },
    ];

    return {
      totalMoney: "",// 总交易金额
      operMoney: '', // 已操作
      notOperMoney: '',// 未操作
      dg: {
        dgName: 'dg',
        tbName: 'salaryListTb',
        total: 0,
        list: [],
        srcCols: cols,
        columns: [],
        selRows: [],
        ps: { // 过滤参数
          page: 1, rows: 20,
          // 查询起止日期
          sedate: [],
          // 审核状态
          salaryStatus: '',
          // 员工用户名
          driverInfo: '',
          // 车牌号
          plateNum: '',
          // 订单号/订单详情
          orderInfo: '',
          // 收支状态
          status: "",
        },
      },

      // 工资状态
      salaryStatusArr: [
        { label: '未操作', value: 'UNOPER' },
        { label: '已生成员工报账', value: 'YSCYGBZ' },
      ],

      // 员工-选择数据列表
      staffOptions: [],
      // 车牌号-选择数据列表
      plateNumOptions: []
    }
  },
  created() {
    const that = this;

    that.initTbSetData(that.dg.dgName);
    that.getDgList();

    that.init()
  },
  methods: {
    init() {
      const that = this;

      // 获取-员工列表
      commonSer.getStaffCombo({ isContainDriver: 1 }).then(res => {
        let list = res?.data || [];

        that.staffOptions = list.map(it => ({
          leaveUname: it.baseUserId.uname,
          realName: it.baseUserId.realName,
          phone: it.baseUserId.phone,
        }));
      });

      // 获取-车牌号列表
      logisticsSer.getAllPlateNum({ status: 0 }).then(res => {
        let plateNumMap = res?.plateNums || {};
        that.plateNumOptions = Object.keys(plateNumMap).map(it => ({ label: it, value: it }));
      });
    },
    // 获取-表格列表
    getDgList() {
      const that = this, U = that.U, dg = that.dg, ps = U.scopy(dg.ps);

      ps.stime = ps.sedate?.length == 2 ? ps.sedate[0] : '';
      ps.etime = ps.sedate?.length == 2 ? ps.sedate[1] : '';
      ps.atype = 10
      ps.type = 17
      delete ps.sedate;

      orderSer.getCompanyWalletRecord(ps).then(res => {
        if (res?.code != 1) {
          U.qerror(res.msg);
        } else {
          console.log(res, '=----------')
          let list = res?.data || [];
          list.forEach(it => { it.dgName = dg.dgName; });

          dg.list = list;
          that.totalMoney = res.statics.totalMoney
          that.operMoney = res.statics.operMoney
          that.notOperMoney = res.statics.notOperMoney
          dg.total = res?.count || 0;
        }
      });
    },

    // 工资excel模板下载
    salaryExcelTpDown() {
      orderSer.exportSalary().then(res => {
        downFiles(res, '工资表-模板');
      });
    },
    // 工资余额导入
    salaryBalanceImport() {
      this.$refs.inputRef.value = null;
      this.$refs.inputRef.click();
    },
    // 生成员工报账
    staffBzExport() {
      const that = this, U = that.U, rows = that.getSeledRows();

      if (rows.length === 0) {
        U.qerror("请至少选择一个员工信息");
      } else {
        U.qconfim('确认生成员工报账？').then(r => {
          if (r) {
            let ids = rows.map(it => it.id).join(',');
            orderSer.createStaffReim({ ids: ids }).then(res => {
              if (res?.code != 1) {
                U.qerror(res.msg);
              } else {
                U.qsuccess(res.msg);

                that.getDgList();
              }
            });
          }
        });
      }
    },

    // 选择文件处理
    handleChange(e) {
      const that = this, U = that.U;

      let file = e.target.files[0];
      let form = new FormData();
      form.append('salaryFile', file);

      orderSer.importSalary(form).then(res => {
        if (res.code !== 1) {
          U.qerror(res.msg);
        } else {
          U.qsuccess(res.msg);

          // 刷新列表
          that.getDgList();
          that.$refs.inputRef.value = null;
        }
      });
    },


  }
}
</script>
